﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyComboBox
{
    public partial class MyComboBox : UserControl
    {
        public MyComboBox()
        {
            InitializeComponent();
        }

        protected string itemText = "";
        [Category("自定义"), Description("显示文本")]
        [Browsable(true)]
        public string Text
        {
            get
            {
                itemText = myTextBox.Text;
                return itemText;
            }
            set
            {
                itemText = value;
                myTextBox.Text = itemText;
            }
        }

        protected ComboBox.ObjectCollection items = null;
        [Category("自定义"), Description("项集合")]
        [Browsable(true)]
        public ComboBox.ObjectCollection Items
        {
            get
            {
                items = comboBox.Items;
                return items;
            }
            set
            {
                ComboBox.ObjectCollection collection = new ComboBox.ObjectCollection(comboBox);
                collection = value;
                foreach (Object item in collection)
                {
                    comboBox.Items.Add(item);
                }
            }
        }

        protected object dataSource = null;
        public object DataSource
        {
            get
            {
                dataSource = comboBox.DataSource;
                return dataSource;
            }
            set
            {
                dataSource = value;
                comboBox.DataSource = dataSource;
            }
        }

        public object SelectedItem
        {
            get
            {
                return comboBox.SelectedItem;
            }
            set
            {
                comboBox.SelectedItem = value;
            }
        }

        public object SelectedValue
        {
            get
            {
                return comboBox.SelectedValue;
            }
            set
            {
                comboBox.SelectedValue = value;
            }
        }

        public string SelectedText
        {
            get
            {
                return comboBox.SelectedText;
            }
            set
            {
                comboBox.SelectedText = value;
            }
        }

        public void Add(string item)
        {
            if(item != null)
                comboBox.Items.Add(item);
        }

        public void Remove(object value)
        {
            if (value != null)
                comboBox.Items.Remove(value);
        }

        public void RemoveAt(int index)
        {
            comboBox.Items.RemoveAt(index);
        }

        private void userButton1_Click(object sender, EventArgs e)
        {
            comboBox.DroppedDown = !comboBox.DroppedDown;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            myTextBox.Text = comboBox.SelectedItem.ToString();
            Console.WriteLine(myTextBox.Text);
        }

        private void MyComboBox_Resize(object sender, EventArgs e)
        {
            myTextBox.Location = new Point(0,0);
            myTextBox.Size = new Size(this.Size.Width,20);
            myButton.Location = new Point(this.Size.Width - 19, 1);
        }


    }
}
